<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="手机号码" prop="mobile">
				<el-input v-model="form.mobile" disabled placeholder="请填写手机号码" clearable></el-input>
			</el-form-item>
			<el-form-item label="姓名" prop="realname">
				<el-input v-model="form.realname" placeholder="请输入完整的真实姓名" clearable></el-input>
			</el-form-item>
			<el-form-item label="昵称" prop="nickname">
				<el-input v-model="form.nickname" placeholder="请输入昵称" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="form.status">
					<el-radio :label="1">正常</el-radio>
					<el-radio :label="0">禁用</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
			<template v-if="mode=='add'">
				<el-form-item label="登录密码" prop="password">
					默认密码：123456
				</el-form-item>
			</template>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id:0,
				mobile: "",
				nickname: "",
				realname: "",
				remark: "",
				status: 1
			},
			//验证规则
			rules: {
				account: [
					{ required: true, message: '请输入登录账号', trigger: 'blur' },
					{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
				],
			},
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				label: "name",
				multiple: true,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		async getGroup(){
			var res = await this.$API.gdshop.role.list();
			this.groups = res.data;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let submitData = {};
					Object.assign(submitData, this.form);
					let methodName;
					if (this.form.id > 0) {
						methodName = 'update'
					}
					else {
						methodName = 'add'
					}
					let res = await this.$API.member.member[methodName](submitData);
					this.isSaveing = false;
					if(res.code === 0){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}else{
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data){
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// console.log(data)
			// Object.assign(this.form, data)
			this.isSaveing = true;
			let res = await this.$API.member.member.info({id: data.id})
			console.log(res)
			this.form = res.data
			this.isSaveing = false;
		}
	}
}
</script>

<style>
</style>
